﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-20XX，米兔网络                                         *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2018年9月4日 15:30:33
 *  文件描述:目标工厂
 *****************************************************************************/

using System;

namespace BrainSpace
{
    //目标类型
    public enum GoalType
    {
        Standby = 1,//待机
    }

    public enum ActionsType
    {
        condition = 1,//条件
        execute = 2,  //执行
        monitor = 3,//监听
    }

    public enum ConditionType//条件类型
    {                       //条件参数(
        Execution_1 = 1,    //1.直接执行.                                1...
    }

    public enum ExecuteType//执行类型
    {
        Execution_1 = 1,      //1.什么也不做.                    执行参数(1.)
        Goal_2 = 2,           //创建一个新的目标在执行里面
        Move_3 = 3,
    }

    public enum MonitorType//监听类型
    {
        Attack_1,//攻击
        Guarded_2,//警戒
    }

    public enum ClusterExecuteType
    {
        Parallel = 1,//并行
        Serial = 2,//串行
        Select = 3,//指定执行
    }

    public enum Source//指令来源
    {
        Decision = 1,//自身决策
        Order = 2,//外部
    }

    //步骤工厂
    public static class GoalPlant
    {
        #region

        public static string toName(this GoalType type)
        {
            string n = string.Empty;
            switch (type)
            {
                default:
                    break;
            }
            return n;
        }

        public static string toName(this ConditionType type)
        {
            string n = string.Empty;
            switch (type)
            {
                case ConditionType.Execution_1: n = "1.直接执行."; break;
                default:
                    break;
            }
            return n;
        }

        public static string toName(this ActionsType type)
        {
            string n = string.Empty;
            switch (type)
            {
                case ActionsType.condition: n = "监听"; break;
                case ActionsType.execute: n = "执行"; break;
            }
            return n;
        }

        public static string toName(this ExecuteType type)
        {
            string n = string.Empty;
            switch (type)
            {
                case ExecuteType.Execution_1: n = "1.什么也不做."; break;
                default:
                    break;
            }
            return n;
        }

        #endregion

        #region

        //创建节点步骤
        public static Goal CreateGoal(int GoalId, Brain guide)
        {
            Goal steps = new Goal(guide);
            var goalData = CreateGoalData(GoalId);
            steps.InitData(goalData);
            return steps;
        }

        public static Goal CreateGoal(Order order, Brain guide, Action over)
        {
            var steps = CreateGoal(order.m_id, guide);
            steps.m_Overs = over;

            return steps;
        }

        //创建行为
        public static Actions CreateActions(ActionsType ctype, int id)
        {
            Actions actions = new Actions();
            switch (ctype)
            {
                case ActionsType.condition:
                    actions = CreateCondition(id);
                    break;

                case ActionsType.execute:
                    actions = CreateExecute(id);
                    break;

                default:
                    break;
            }
            return actions;
        }

        //创建监听
        public static Condition CreateCondition(int type)
        {
            Condition condition = new Condition();
            switch (type.toEnum<ConditionType>())
            {
                case ConditionType.Execution_1:
                    break;

                default:
                    break;
            }
            return condition;
        }

        //创建执行
        public static Execute CreateExecute(int types)
        {
            Execute execute = new Execute();
            switch (types.toEnum<ExecuteType>())
            {
                case ExecuteType.Execution_1:
                    break;

                case ExecuteType.Goal_2:

                    break;

                case ExecuteType.Move_3:
                    break;

                default:
                    break;
            }
            return execute;
        }

        #endregion

        //创建目标数据
        public static GoalData CreateGoalData(int GoalId)
        {
            GoalData data = new GoalData();
            data.m_goalInfo = new GoalInfoCfg();
            data.m_type = data.m_goalInfo.Type.toEnum<GoalType>();
            data.m_condition = CreateActionClusteData(ActionsType.condition, data);
            data.m_execute = CreateActionClusteData(ActionsType.execute, data);

            return data;
        }

        public static ActionClusteData CreateActionClusteData(ActionsType ctype, GoalData goalData)
        {
            ActionClusteData data = new ActionClusteData();
            data.m_ActionsType = ctype;
            switch (ctype)
            {
                case ActionsType.condition:
                    {
                        data.exType = goalData.m_goalInfo.conditionClusterType.toEnum<ClusterExecuteType>();
                        data.IsAll = goalData.m_goalInfo.conditionFinishType == 1;
                        for (int i = 0; i < goalData.m_goalInfo.conditionID.Count; i++)
                        {
                            int id = goalData.m_goalInfo.conditionID[i];
                            ActionData actData = CreateActionData(id);
                            data.m_ListData.Add(actData);
                        }
                    }
                    break;

                case ActionsType.execute:
                    {
                        data.exType = goalData.m_goalInfo.executeClusterType.toEnum<ClusterExecuteType>();
                        data.IsAll = goalData.m_goalInfo.executeFinishType == 1;
                        for (int i = 0; i < goalData.m_goalInfo.executeID.Count; i++)
                        {
                            int id = goalData.m_goalInfo.executeID[i];
                            ActionData actData = CreateActionData(id);
                            data.m_ListData.Add(actData);
                        }
                    }
                    break;

                default:
                    break;
            }
            return data;
        }

        public static ActionData CreateActionData(int id)
        {
            ActionData data = new ActionData();
            var lin = ActionInfoCfgMgr.Instance.GetDataByid(id);
            switch (lin.type.toEnum<ActionsType>())
            {
                case ActionsType.condition:
                    switch (lin.essenceType.toEnum<ConditionType>())
                    {
                        case ConditionType.Execution_1:
                            break;
                    }
                    break;

                case ActionsType.execute:
                    switch (lin.essenceType.toEnum<ExecuteType>())
                    {
                        case ExecuteType.Execution_1:
                            break;

                        case ExecuteType.Goal_2:
                            data = new GoalConditionData();
                            break;
                    }
                    break;

                case ActionsType.monitor:
                    switch (lin.essenceType.toEnum<MonitorType>())
                    {
                        default:
                            data = new MonitorData();
                            break;
                    }
                    break;
            }

            data.m_id = id;
            data.m_data = lin;
            data.Init();
            return data;
        }

        public static T ActionDataSwitch<T>(this ActionData data) where T : ActionData
        {
            T t = data as T;
            return t;
        }

        //创建监听类型
        public static Monitor CreateMonitor(int id, Decision brain)
        {
            Monitor monitor = new Monitor();
            var actionData = CreateActionData(id);
            MonitorData data;
            switch (actionData.m_data.essenceType.toEnum<MonitorType>())
            {
                //case MonitorType.Attack_1:
                //    data = actionData.ActionDataSwitch<AttackMonitor>();
                //    break;
                //
                //case MonitorType.Guarded_2:
                //    break;

                default:
                    data = actionData.ActionDataSwitch<MonitorData>();
                    break;
            }
            data.Init();
            monitor.m_brain = brain;
            monitor.m_data = data;
            return monitor;
        }
    }
}